package ru.spbau.database.group3.parser;

/**
 * Created with IntelliJ IDEA.
 * User: stas
 * Date: 1/31/13
 * Time: 9:57 PM
 */
public class RecordConstraint implements Constraint {

    public RecordConstraint(String colName , String value, String cmp) {
        this.colName = colName;
        this.value = value;
        this.cOp = ConditionOperator.createConstraintOperator(cmp);
    }
    @Override
    public String getColName() {
        return colName;
    }
    @Override
    public String getValue() {
        return value;
    }

    public static Constraint createConstraint(String s) {
        for(String cs : StringConstants.CTS) {
            if(s.indexOf(cs) > 0 && s.indexOf(cs) + cs.length() < s.length()) {
                String colName = s.substring(0, s.indexOf(cs)).trim();
                String value = s.substring(s.indexOf(cs) + cs.length()).trim();
                return new RecordConstraint(colName, value, cs);
            }
        }
        throw new IllegalArgumentException("Wrong constraint " + s);
    }

    @Override
    public ConditionOperator getConditionOperator() {
        return cOp;
    }

    @Override
    public String toString() {
        return "RecordConstraint{" +
                "colName='" + colName + '\'' +
                ", value='" + value + '\'' +
                ", cOp=" + cOp +
                '}';
    }


    private String colName;
    private String value;
    private ConditionOperator cOp;
}
